package it.stasbranger.myproject.service;

import it.stasbranger.myproject.dao.RequestsDAO;
import it.stasbranger.myproject.dao.SuppliersHasRequestsDAO;

import it.stasbranger.myproject.domain.Requests;
import it.stasbranger.myproject.domain.SuppliersHasRequests;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Requests entities
 * 
 */

@Service("RequestsService")
@Transactional
public class RequestsServiceImpl implements RequestsService {

	/**
	 * DAO injected by Spring that manages Requests entities
	 * 
	 */
	@Autowired
	private RequestsDAO requestsDAO;

	/**
	 * DAO injected by Spring that manages SuppliersHasRequests entities
	 * 
	 */
	@Autowired
	private SuppliersHasRequestsDAO suppliersHasRequestsDAO;

	/**
	 * Instantiates a new RequestsServiceImpl.
	 *
	 */
	public RequestsServiceImpl() {
	}

	/**
	 * Return all Requests entity
	 * 
	 */
	@Transactional
	public List<Requests> findAllRequestss(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Requests>(requestsDAO.findAllRequestss(startResult, maxRows));
	}

	/**
	 * Delete an existing SuppliersHasRequests entity
	 * 
	 */
	@Transactional
	public Requests deleteRequestsSuppliersHasRequestses(Integer requests_idrequests, Integer related_suppliershasrequestses_suppliersIdsuppliers, Integer related_suppliershasrequestses_requestsIdrequests) {
		SuppliersHasRequests related_suppliershasrequestses = suppliersHasRequestsDAO.findSuppliersHasRequestsByPrimaryKey(related_suppliershasrequestses_suppliersIdsuppliers, related_suppliershasrequestses_requestsIdrequests, -1, -1);

		Requests requests = requestsDAO.findRequestsByPrimaryKey(requests_idrequests, -1, -1);

		related_suppliershasrequestses.setRequests(null);
		requests.getSuppliersHasRequestses().remove(related_suppliershasrequestses);

		suppliersHasRequestsDAO.remove(related_suppliershasrequestses);
		suppliersHasRequestsDAO.flush();

		return requests;
	}

	/**
	 * Save an existing SuppliersHasRequests entity
	 * 
	 */
	@Transactional
	public Requests saveRequestsSuppliersHasRequestses(Integer idrequests, SuppliersHasRequests related_suppliershasrequestses) {
		Requests requests = requestsDAO.findRequestsByPrimaryKey(idrequests, -1, -1);
		SuppliersHasRequests existingsuppliersHasRequestses = suppliersHasRequestsDAO.findSuppliersHasRequestsByPrimaryKey(related_suppliershasrequestses.getSuppliersIdsuppliers(), related_suppliershasrequestses.getRequestsIdrequests());

		// copy into the existing record to preserve existing relationships
		if (existingsuppliersHasRequestses != null) {
			existingsuppliersHasRequestses.setSuppliersIdsuppliers(related_suppliershasrequestses.getSuppliersIdsuppliers());
			existingsuppliersHasRequestses.setRequestsIdrequests(related_suppliershasrequestses.getRequestsIdrequests());
			existingsuppliersHasRequestses.setSenddate(related_suppliershasrequestses.getSenddate());
			existingsuppliersHasRequestses.setPrice(related_suppliershasrequestses.getPrice());
			related_suppliershasrequestses = existingsuppliersHasRequestses;
		}

		related_suppliershasrequestses.setRequests(requests);
		requests.getSuppliersHasRequestses().add(related_suppliershasrequestses);
		related_suppliershasrequestses = suppliersHasRequestsDAO.store(related_suppliershasrequestses);
		suppliersHasRequestsDAO.flush();

		requests = requestsDAO.store(requests);
		requestsDAO.flush();

		return requests;
	}

	/**
	 * Save an existing Requests entity
	 * 
	 */
	@Transactional
	public void saveRequests(Requests requests) {
		Requests existingRequests = requestsDAO.findRequestsByPrimaryKey(requests.getIdrequests());

		if (existingRequests != null) {
			if (existingRequests != requests) {
				existingRequests.setIdrequests(requests.getIdrequests());
				existingRequests.setDate(requests.getDate());
				existingRequests.setName(requests.getName());
				existingRequests.setEmail(requests.getEmail());
			}
			requests = requestsDAO.store(existingRequests);
		} else {
			requests = requestsDAO.store(requests);
		}
		requestsDAO.flush();
	}

	/**
	 * Return a count of all Requests entity
	 * 
	 */
	@Transactional
	public Integer countRequestss() {
		return ((Long) requestsDAO.createQuerySingleResult("select count(o) from Requests o").getSingleResult()).intValue();
	}

	/**
	 */
	@Transactional
	public Requests findRequestsByPrimaryKey(Integer idrequests) {
		return requestsDAO.findRequestsByPrimaryKey(idrequests);
	}

	/**
	 * Load an existing Requests entity
	 * 
	 */
	@Transactional
	public Set<Requests> loadRequestss() {
		return requestsDAO.findAllRequestss();
	}

	/**
	 * Delete an existing Requests entity
	 * 
	 */
	@Transactional
	public void deleteRequests(Requests requests) {
		requestsDAO.remove(requests);
		requestsDAO.flush();
	}
}
